import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import antd from 'ant-design-vue'
import {message} from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'
import axios from 'axios'
import moment from 'moment'
import Cookies from 'js-cookie'
// 一个“包装”了 UEditor 的 Vue 组件
import VueUeditorWrap from 'vue-ueditor-wrap'
const app = createApp(App)
app.use(store).use(router).use(antd).use(VueUeditorWrap).mount('#app')
app.config.globalProperties.$moment = moment

axios.defaults.baseURL = 'http://localhost:8086'
axios.defaults.withCredentials = true

axios.interceptors.request.use(
    (config)=>{
        if (config.url == '/getCode' || config.url == '/login'){
            return config
        }
        // //1、过滤所有的请求、如果不是get请求则对数据进行处理
        // if (config.method != 'get') {
        //     //对请求的数据进行处理，然后回set请求对象中
        //     config.data = qs.stringify(config.data);
        // }
        // config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        //拦截的判断逻辑
        const user = Cookies.get("user");
        if (!user) {
            //如果用户信息为空，就是未登录--使用路由跳转到登录页面
            message.warn("登录验证失效，请重新登录")
            router.push("/");
        }
        return config;
    },
    (error)=>{
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(
    (response)=>{
        return response
    },
    (error)=>{
        if (error.code == 'ERR_NETWORK'){
            message.error("登录失效,请重新登录")
            router.push("/")
        }
        return Promise.reject(error)
    }
)
